/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: happyiu
 * Date: 2021-11-17
 * Time: 20:11
 */

class ListNode{
    int val;
    ListNode next;

    public  ListNode(int val){
        this.val = val;
    }
}


public class MyLinkedList {

    public ListNode head;
    public void display() {
        //this.head.next != null
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }




    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }


    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addLast(int data){
        ListNode node = new ListNode(data);

        if(this.head == null){
            this.head=node;
        }
        else{
            ListNode cur = this.head;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }

    }



}
